package com.clas.liu.myapplication.view.activity.impl;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.model.entity.notice.Notice;
import com.clas.liu.myapplication.presenter.impl.BasePresenter;
import com.clas.liu.myapplication.presenter.impl.PublishNoticePresenter;
import com.clas.liu.myapplication.view.activity.i.IPublishNoticeActivity;

/**
 * Created by liu on 2019/5/22.
 */

public class PublishNoticeActivity extends BaseActivity<PublishNoticePresenter> implements IPublishNoticeActivity,View.OnClickListener{

    private ImageView back;
    private ImageView submit;
    private EditText editText;
    private ProgressBar progressBar;
    private Notice notice;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_notice_layout);
        back = findViewById(R.id.publish_notice_back_bt);
        submit = findViewById(R.id.publish_notice_submit);
        editText = findViewById(R.id.publish_notice_et);
        progressBar = findViewById(R.id.publish_notice_submit_pb);
        setListener();
    }

    private void setListener() {
        back.setOnClickListener(this);
        submit.setOnClickListener(this);
    }
    private void startPost() {
        showProgressBar();
        if (notice == null) {
            notice = new Notice();
        }
        notice.setText(editText.getText().toString());
        notice.setTime(String.valueOf(System.currentTimeMillis()));
        mPresenter.postNotice(notice);
    }
    private void showProgressBar() {
        progressBar.setVisibility(View.VISIBLE);
        submit.setVisibility(View.GONE);
    }
    private void hideProgressBar() {
        progressBar.setVisibility(View.GONE);
        submit.setVisibility(View.VISIBLE);
    }

    @Override
    public void failBecauseNetReturn(int code) {
        hideProgressBar();
        showToast(code+"请求失败！", Toast.LENGTH_SHORT);
    }

    @Override
    public void postReturn() {
        hideProgressBar();
        showToast("发布成功！", Toast.LENGTH_SHORT);
        finish();
    }

    @Override
    protected PublishNoticePresenter getPresenter() {
        return new PublishNoticePresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.publish_notice_back_bt:
                finish();
                break;
            case R.id.publish_notice_submit:
                startPost();
                break;
        }
    }
}
